﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Business;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Asn1.Cms;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
   // [Authorize]
    [ApiController]
    public class WorkShopController : ControllerBase
    {
        private IWorkShopService _workShopService;

        public WorkShopController(IWorkShopService workShopService)
        {
            _workShopService = workShopService;
        }
        [HttpPost]
        public ApiResponseData GetWorkShopData(WorkShopInput input)
        {
            List<WorkShopModel> ModelList = _workShopService.GetAllWorkShopModel(); ;
            if (!input.workshopId.IsNullOrEmpty())
            {
                ModelList = ModelList.Where(i => i.WorkShopId.Contains(input.workshopId)).ToList(); 
            }
            int count = ModelList.Count;
            if (!ModelList.IsNullOrEmpty())
            {
                int start = (input.page - 1) * input.size;
                int end = ((start + input.size) >= (ModelList.Count)) ? (ModelList.Count) : (start + input.size);
                ModelList = ModelList.GetRange(start, end - start);
            }
            return ResultHelper.Success(new ReturnData() { total = count, dataList = ModelList });
        }
        [HttpGet]
        public ApiResponseData GetAllWorkShopData()
        {
            List<WorkShop> model = _workShopService.GetAllWorkShop();
            return ResultHelper.Success(new ReturnData() { total = model.Count, dataList = model });
        }
        [HttpPost]
        public ApiResponseData AddWorkShop(WorkShop model)
        {
            //判断重复
            if(_workShopService.GetWorkShopById(model.WorkShopId) != null)
            {
                return ResultHelper.Error(502, "ID cannot be repeated! ");
            }
            if (_workShopService.AddSingleWorkShop(model))
            {
                return ResultHelper.Success("Add successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to add!");
            }
        }
        [HttpPost]
        public ApiResponseData UpdateWorkShop(WorkShop model)
        {
            if (_workShopService.GetWorkShopById(model.WorkShopId) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + model.WorkShopId + "!");
            }
            if(_workShopService.UpdateWorkShop(model))
            {
                return ResultHelper.Success("Update successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Update!");
            }

        }
        [HttpPost]
        public ApiResponseData DeleteWorkShop(DeleteStringId id)
        {
            if (_workShopService.GetWorkShopById(id.id) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + id + "!");
            }
            if (_workShopService.DeleteWorkShop(id.id))
            {
                return ResultHelper.Success("Delete successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Delete!");
            }
        }
        [HttpPost]
        public ApiResponseData BatchDelWorkShop(List<string> batchRemoveDatas)
        {
            if(batchRemoveDatas.Count > 0)
            {
                if (_workShopService.BatchDelWorkShop(batchRemoveDatas))
                {
                    return ResultHelper.Success("Delete successfully!");
                }
                else
                {
                    return ResultHelper.Error(500, "Failed to Delete!");
                }
            }
            return ResultHelper.Error(502, "No data detected!");
        }
    }
}
